<?php
 
function search($crit)
{
	
	$crit = trim($crit);
	//echo $crit."<br>" ;
	
	if(empty($crit)) 
	{ 
		$crit = "*";
	} 

	if ( $crit == "*" )	
	$search_string = "'\(";

	else
	{
		$search_string="'\(";		
		$crit_search_string = explode(" ", $crit); 
		$len = sizeof($crit_search_string)-1;
	 //	echo $len."<br>" ;

	

		if ( $len != 0  )
		{
			for($i=0; $crit_search_string[$i]; $i++) 
			{ 
		
				if ($i != $len) 
				{
				    	$search_string = $search_string.$crit_search_string[$i]."\|";
	//				echo "----".$search_string ;
				} 
				elseif ($i = $len) 
				{
    					$search_string = $search_string.$crit_search_string[$i]."\)'";
	//				echo "----".$search_string ;
				} 
	
			}
 	
		}
		else
		$search_string = $crit;
	
		$commande = 'sudo -u www-data bash -c "cat res | grep '.$search_string.' > tmp"';
	//	echo "----".$search_string."<br>" ;
	//	echo $commande."<br>" ;
		exec($commande,$res);
		$i = 0;
		
		foreach ($res as $line)
		{
			$ID = explode("|", $line); 
			if ( $i == 0 )
				$commande = 'sudo -u www-data bash -c "cat res | grep '.$ID[0].' > tmp"';
			else
				$commande = 'sudo -u www-data bash -c "cat res | grep '.$ID[0].' >> tmp"';
			 
	//		echo $commande."<br>" ;
			exec($commande,$res);	
			$i++;

		}
		$i = 0;

		$commande = 'sudo -u www-data bash -c "cat tmp > res"';
	//	echo $commande."<br>" ;
		exec($commande,$res);


		foreach ($res as $line)
		{
			$ID = explode("|", $line); 
			if ( $i == 0 )
				$commande = 'sudo -u www-data bash -c "cat res | grep '.$ID[0].' > tmp"';
			else
				$commande = 'sudo -u www-data bash -c "cat res | grep '.$ID[0].' >> tmp"';
			 
	//		echo $commande."<br>" ;
			exec($commande,$res);	

			$i++;

		}



		$commande = 'sudo -u www-data bash -c "rm -rf tmp"';
	//	echo $commande."<br>" ;
		exec($commande,$res); 
		$search_string = "'\(";
	}

}
////////////////////////////////////////
 
//////////////*****************************************
$year = $_POST['Year'] ;
$month = $_POST['Month'] ;
$day = $_POST['Day'] ;
$user = $_POST['User'] ;
$status = $_POST['Status'] ;
$filename = $_POST['Filename'] ;
$app = $_GET['App'] ;
 
if(empty($year)) 
{ 
$year="*";
} 
 
if(empty($month)) 
{ 
$month="*";
}

if(empty($day)) 
{ 
$day="*";
}

if(empty($user)) 
{ 
$user="*";
}

if(empty($status)) 
{ 
$status="*";
}

if(empty($filename)) 
{ 
$filename="*";
}

if(empty($app)) 
{ 
$app="*";
}


$index_path="/var/www/FIEM/norm_log/".$year."/".$month."/".$day."/event.log";

//echo( "Votre path: <b>".$index_path."</b><br>" ) ;
 

$commande= 'sudo -u www-data bash -c "locate -d /var/www/FIEM/fiem.db '.$index_path.' | xargs cat > res" ' ;
 echo $commande."<br>" ;
exec($commande);

 
search($user);

search($status);
 
search($app);
 
search($filename);

$commande= 'sudo -u www-data bash -c "cat res |wc -l" ' ;
//echo $commande."<br>" ;
exec($commande, $res);

 
if( $res[0] == 0 ) 
{ 
	echo '<h2>No result</h2>';
}
else
{
	echo '<div id="display"></div>';
}
//header( "Location: search.html" ); 
?>
